package org.finesys.system.controller;

import java.util.List;

import org.finesys.common.core.constants.RestApiConfig;
import org.finesys.common.core.exception.ErrorCodes;
import org.finesys.common.core.module.R;
import org.finesys.common.core.util.MessageUtil;
import org.finesys.common.log.core.annotation.LogAnnotation;
import org.finesys.common.log.core.enums.OperateType;
import org.finesys.common.security.core.annotation.Inner;
import org.finesys.system.api.dto.SysUserDTO;
import org.finesys.system.api.dto.UserGiteeDto;
import org.finesys.system.api.entity.SysUser;
import org.finesys.system.api.entity.SysUserGitee;
import org.finesys.system.constants.ControllerRestApiConfig;
import org.finesys.system.convert.SysUserGiteeConvert;
import org.finesys.system.service.SysUserService;
import org.finesys.system.service.SysUsersGiteeService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping(ControllerRestApiConfig.GITEE)
public class SysUserGiteeController {

    private final SysUsersGiteeService sysUsersGiteeService;
    private final SysUserService sysUserService;

    /**
     * 新增gitee信息表
     *
     * @param userGiteeDto gitee信息表
     * @return R
     */
    @Operation(summary = "新增gitee信息", description = "新增gitee信息表")
    @LogAnnotation(title = "新增gitee信息", operateType = OperateType.INSERT)
    @PostMapping
    public R<Boolean> save(@RequestBody UserGiteeDto userGiteeDto) {
        return R.success(sysUsersGiteeService.addSysUsersGitee(userGiteeDto));
    }

    /**
     * 修改gitee信息表
     *
     * @param userGiteeDto gitee信息表
     * @return R
     */
    @Operation(summary = "修改gitee信息表", description = "修改gitee信息表")
    @LogAnnotation(title = "修改gitee信息表", operateType = OperateType.UPDATE)
    @PutMapping(value = RestApiConfig.UPDATE)
    public R<Boolean> updateById(@RequestBody UserGiteeDto userGiteeDto) {
        return R.success(sysUsersGiteeService.update(userGiteeDto));
    }

    /**
     * 通过id删除gitee信息表
     *
     * @param roleId id
     * @return R
     */
    @Operation(summary = "通过id删除gitee信息表", description = "通过id删除gitee信息表")
    @LogAnnotation(title = "删除gitee", operateType = OperateType.DELETE)
    @DeleteMapping(value = RestApiConfig.DELETE)
    public R<Boolean> removeById(@PathVariable Long roleId) {
        return R.success(sysUsersGiteeService.deleteGitee(roleId));
    }

    /**
     * 通过id查询gitee信息表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping(RestApiConfig.GET)
    public R<UserGiteeDto> getById(@PathVariable("id") Long id) {
        return R.success(sysUsersGiteeService.findById(id));
    }


    /**
     * 获取gitee列表
     *
     * @return gitee列表
     */
    @GetMapping(RestApiConfig.ALL)
    public R<List<UserGiteeDto>> findAll() {
        return R.success(SysUserGiteeConvert.INSTANCE.convertList(sysUsersGiteeService.findAll()));
    }

    /**
     * 获取gitee列表
     *
     * @return gitee列表
     */
    @GetMapping(RestApiConfig.LIST)
    public R<List<UserGiteeDto>> findList(UserGiteeDto userGiteeDto) {
        SysUserGitee sysUserGitee = SysUserGiteeConvert.INSTANCE.convert(userGiteeDto);
        return R.success(SysUserGiteeConvert.INSTANCE.convertList(sysUsersGiteeService.findList(sysUserGitee)));
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping(RestApiConfig.PAGE)
    public R<Page<UserGiteeDto>> getPage(Page<SysUserGitee> page, UserGiteeDto userGiteeDto) {
        SysUserGitee sysUserGitee = SysUserGiteeConvert.INSTANCE.convert(userGiteeDto);
        return R.success(SysUserGiteeConvert.INSTANCE.convertPage(sysUsersGiteeService.findPage(page, sysUserGitee)));
    }


    /**
     * 绑定
     */
    @Inner(value = true)
    @Operation(summary = "绑定")
    @PostMapping(value = "/binding")
    public R<Boolean> binding(@RequestBody UserGiteeDto userGiteeDto) {
        return R.success(sysUsersGiteeService.binding(userGiteeDto));
    }


    /**
     * 获取单条
     */
    @Inner(value = true)
    @Operation(summary = "获取单条")
    @GetMapping(value = "/getByAppidAndId")
    public R<UserGiteeDto> getByAppidAndId(@RequestParam("appId") String appId, @RequestParam("id") Integer id) {
        return R.success(sysUsersGiteeService.getByAppidAndId(appId, id));
    }

    /**
     * 获取用户
     */
    @Inner(value = true)
    @Operation(summary = "获取用户")
    @GetMapping(value = "/getUser")
    public R<SysUserDTO> getUser(@RequestParam("appId") String appId, @RequestParam("id") Integer id) {
        return R.success(sysUsersGiteeService.getUser(appId, id));
    }

    /**
     * 获取指定用户全部信息
     *
     * @return 用户信息
     */
    @Inner
    @GetMapping(value = {"/info/query"})
    public R<?> info(@RequestParam(required = false) String appId) {
        SysUserGitee sysUserGitee = sysUsersGiteeService.getOne(Wrappers.<SysUserGitee>query()
                .lambda()
                .eq(StrUtil.isNotBlank(appId), SysUserGitee::getAppId, appId));

        if (sysUserGitee == null) {
            return R.failed(MessageUtil.getMessage(ErrorCodes.SYS_USER_GITEE_USERINFO_EMPTY, appId));
        }
        SysUser user = sysUserService.getById(sysUserGitee.getUserId());
        if (user == null) {
            return R.failed(MessageUtil.getMessage(ErrorCodes.SYS_USER_USERINFO_EMPTY, appId));
        }
        return R.success(sysUserService.getUserInfo(user));
    }

}
